package cn.yuan.xiaoyu.jokemodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;
import cn.yuan.xiaoyu.commonmodule.view.recycleview.YRecycleview;
import cn.yuan.xiaoyu.jokemodule.adapter.JokeListAdapter;
import cn.yuan.xiaoyu.jokemodule.bean.JokeListBean;
import cn.yuan.xiaoyu.jokemodule.presenter.IjokView;
import cn.yuan.xiaoyu.jokemodule.presenter.JokePresenter;

/**
 * Created by yukuoyuan on 2017/2/27.
 * 这是一个笑话列表的界面
 */
public class JokeActivity extends BaseActivity implements IjokView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toolbar_left)
    ImageView ivToolbarLeft;
    @Bind(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @Bind(R.id.rcv_joke)
    YRecycleview rcvJoke;
    private JokePresenter jokePresenter;
    private JokeListAdapter jokeListAdapter;
    private List<JokeListBean.ResultBean.DataBean> jokeListBeans = new ArrayList<>();
    private int page = 1;

    @Override
    protected void bindPresenter() {
        jokePresenter = new JokePresenter(this, this);
    }

    @Override
    protected void initData(Bundle extras) {
        showL("正在加载");
        rcvJoke.setLayoutManager(new LinearLayoutManager(this));
        jokePresenter.getJokeList(page);
        rcvJoke.setRefreshAndLoadMoreListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_joke;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToolbarLeft.setVisibility(View.VISIBLE);
        tvToolbarTitle.setVisibility(View.VISIBLE);
        tvToolbarTitle.setText("笑话大全");
    }


    @OnClick({R.id.iv_toolbar_left, R.id.iv_toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_left:
                onBackPressed();
                break;
            case R.id.iv_toolbar_right:
                break;
        }
    }

    @Override
    public void showT(String msg) {

    }

    @Override
    public void showL(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeL() {
        closeLoad();
    }

    @Override
    public void setAdapter(JokeListBean jokeListBean) {
        List<JokeListBean.ResultBean.DataBean> dataBean = jokeListBean.result.data;
        jokeListBeans.addAll(dataBean);
        if (jokeListAdapter == null) {
            jokeListAdapter = new JokeListAdapter(jokeListBeans);
            rcvJoke.setAdapter(jokeListAdapter);
        } else {
            jokeListAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void complete() {
        rcvJoke.setReFreshComplete();
        rcvJoke.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        page = 1;
        jokePresenter.getJokeList(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        Log.d("第几页", page + "");
        jokePresenter.getJokeList(page);
    }
}
